package com.qf.mvc01.controller;

import com.qf.mvc01.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class RestfullController {

    @Autowired
    private User user1;

    @Autowired
    private User user2;

    @GetMapping("/users")
    public List<User> getAllUser(){
        List<User> users = Arrays.asList(user1,user2);
        return users;
    }

    /**
     * 新增用户接口设计
     * 需要考虑 post请求体中的数据格式
     * 例如：  id=100&name=zhagnsan&salary=1000
     */
    @PostMapping("/users")
    public Map<String,String> addUser(User user){
        System.out.println(user);
        Map<String,String> map = new HashMap<>();
        map.put("code","1");
        map.put("msg","添加成功");
        return map;
    }

    @PutMapping("/users")
    public Map<String,String> updateUser(@RequestBody User user){
        System.out.println(user);
        Map<String,String> map = new HashMap<>();
        map.put("code","1");
        map.put("msg","修改成功");
        return map;
    }

    @DeleteMapping("/users/{id}")
    public Map<String,String> delUser(@PathVariable("id") Integer id){
        System.out.println("删除id为"+id+"的用户");
        Map<String,String> map = new HashMap<>();
        map.put("code","1");
        map.put("msg","删除成功");
        return map;
    }
}
